
#include "QueryDispatcher.h"
#include "../dependency/QueryStructures.h"
#include <iostream>
#include "../dependency/StatusCodes.h"
#include "../dependency/ErrorLookupTable.h"
#include "../sql/CreateDatabase.h"
#include "../sql/OpenDatabase.h"
#include "../sql/ShowTables.h"
#include "../sql/CreateTable.h"
#include "../sql/CreateIndex.h"
#include "../sql/InsertTable.h"
#include "../sql/SelectTable.h"
#include "../dependency/debug.h"
#include "ShowDatabases.h"
#include "DescribeTable.h"
#include "InsertTable.h"
#include "Delete.h"
#include "DropTable.h"
#include "UpdateTable.h"
#include "DropDatabase.h"
#include <string.h>

QueryDispatcher::QueryDispatcher() {

}

QueryDispatcher::~QueryDispatcher() {

}

STATUS_CODE QueryDispatcher::run(QueryStructures *queryStruct) {
    int error = UNKNOWN_ERROR;
    switch (queryStruct->statementType) {

        case QueryStructures::CREATE_DATABASE_STATEMENT:
        {
            // cout << "Create DB";
            CreateDatabase createDatabase;
            error = createDatabase.run(queryStruct);
            break;
        }
        case QueryStructures::SHOW_DATABASES_STATEMENT:
        {
            //cout<<"show DB";
            ShowDatabases sh;
            error = sh.run(queryStruct);
            break;
        }
        case QueryStructures::OPEN_DATABASE_STATEMENT:
        {
            //            cout << "open DB";
            OpenDatabase od;
            error = od.run(queryStruct);
            break;
        }
        case QueryStructures::DROP_DATABASE_STATEMENT:
        {
            //            cout << "drop DB";
            //            		DEBUG("drop database");
            DropDatabase dropDatabase;
            error = dropDatabase.run(queryStruct);
            break;
        }
        case QueryStructures::SHOW_TABLES_STATEMENT:
        {
            //cout << "show tab";
            ShowTables showTables;
            error = showTables.run(queryStruct);
            break;
        }
        case QueryStructures::CREATE_TABLE_STATEMENT:
        {
            //cout << "Create tab";
            CreateTable createTable;
            error = createTable.run(queryStruct);
            break;
        }
        case QueryStructures::INSERT_STATEMENT:
        {
            //cout << "insert tab";
            InsertTable it;
            error = it.run(queryStruct);
            break;
        }
        case QueryStructures::DESCRIBE_STATEMENT:
        {
            //cout << "desc";
            DescribeTable dt;
            error = dt.run(queryStruct);
            break;
        }
        case QueryStructures::SELECT_STATEMENT:
        {
            //  cout << "select tab";
            SelectTable select;
            error = select.run(queryStruct);
            break;
        }
        case QueryStructures::DELETE_STATEMENT:
        {
            //cout << "delete tab";
            Delete deleteTabrec;
            error = deleteTabrec.run(queryStruct);

            break;
        }
        case QueryStructures::UPDATE_STATEMENT:
        {
            //            cout << "update tab";
            UpdateTable update;
            error = update.run(queryStruct);
            break;
        }
        case QueryStructures::DROP_TABLE_STATEMENT:
        {
            //cout << "drop tab";
            DropTable dropTable;
            error = dropTable.run(queryStruct);
            break;
        }
        case QueryStructures::CREATE_INDEX_STATEMENT:
        {
            //cout << "Create index";
            CreateIndex createIndex;
            error = createIndex.run(queryStruct);
            break;
        }
        case QueryStructures::DROP_INDEX_STATEMENT:
        {
            cout << "drop index";
            break;
        }
        default:
        {
            cout << queryStruct->errorMessage;
            strcpy(queryStruct->errorMessage, ErrorLookupTable::lookup(UNKNOWN_ERROR).c_str());
        }

    }

    strcpy(queryStruct->errorMessage,
            ErrorLookupTable::lookup(error).c_str());
    return error;
}
